//
//  MYGResumeViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/12.
//

import UIKit
import QMUIKit

class MYGResumeViewController: MYGBaseController {
    
    private var  contents:[MYGResumType] = []
    private var  mediaContents:[MYGResumType] = []
    private var  isEN:Bool = false
    private var isMedia:Bool = false
    private var doctorResumeModel = MYGResumeModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        confgUI()
       
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getResumList()
    }
    
    func confgUI() {
        title = "我的简历"
        view.backgroundColor = .white        
        view.addSubview(tableview)
        tableview.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        view.addSubview(editBtn)
        view.addSubview(shareBtn)
        view.addSubview(dowBtn)
        
        editBtn.snp.makeConstraints { make in
            make.width.height.equalTo(76)
            make.bottom.equalToSuperview().offset(-198)
            make.right.equalToSuperview().offset(-14)
        }
        
        shareBtn.snp.makeConstraints { make in
            make.width.height.equalTo(76)
            make.bottom.equalToSuperview().offset(-122)
            make.right.equalToSuperview().offset(-14)
        }
        
        dowBtn.snp.makeConstraints { make in
            make.width.height.equalTo(76)
            make.bottom.equalToSuperview().offset(-46)
            make.right.equalToSuperview().offset(-14)
        }
        
    }

    private lazy var tableview: UITableView = {
        let tableview = UITableView(frame: CGRectZero, style: .grouped)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.separatorStyle = .none
        tableview.backgroundColor = .clear
        tableview.showsVerticalScrollIndicator = false
        tableview.tableHeaderView = headerView
        tableview.register(cellWithClass: MYGResumeContentCell.self)
        tableview.register(cellWithClass: MYGMediaTableViewCell.self)
        tableview.register(cellWithClass: MYGResumeOtherCell.self)
        return tableview
    }()
    
    private lazy var headerView:MYGResumeHeaderView = {
        let header = MYGResumeHeaderView()
        header.frame = CGRectMake(0, 0, screenWidth, SCALEWIDTH(194))
        header.CNPersonInfoBlock = {[self] in
            getCNResumInfo()
        }
        header.ENPersonInfoBlock = {[self] in
            getENResumInfo()
        }
        header.mediaPersonInfoBlock = {[self] in
            getMediaResumInfo()
        }
        return header
    }()
    
    private lazy var editBtn: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "re_edit"), for: .normal)
        //btn.backgroundColor = .rgba(255, 227, 227, 0.42).withAlphaComponent(0.5)
        btn.layer.cornerRadius = 76/2
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(clickEditBtn), for: .touchUpInside)
        return btn
    }()
    private lazy var shareBtn: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "re_share"), for: .normal)
        //btn.backgroundColor = .rgba(255, 227, 227, 0.42).withAlphaComponent(0.5)
        btn.layer.cornerRadius = 76/2
        btn.layer.masksToBounds = true
       btn.addTarget(self, action: #selector(clickShareBtn), for: .touchUpInside)
        return btn
    }()
    private lazy var dowBtn: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "re_dow"), for: .normal)
        //btn.backgroundColor = .rgba(255, 227, 227, 0.42).withAlphaComponent(0.5)
        btn.layer.cornerRadius = 76/2
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(exportResumeBtn), for: .touchUpInside)
        return btn
    }()
}
extension MYGResumeViewController {
    
    @objc func clickShareBtn(){
        let vc = MYGResumeShareVC()
        navigationController?.pushViewController(vc)
    }
    @objc func clickEditBtn(){
        
        if isMedia {
           let vc = MYGAddMediaViewController()
            vc.id = "\(doctorResumeModel.media.id)"
            vc.content = doctorResumeModel.media.content
            navigationController?.pushViewController(vc)

        }else {
            let vc = MYGResumeEditController()
            vc.model = self.doctorResumeModel
            vc.configCell()
            navigationController?.pushViewController(vc)
        }
      
    }
    @objc func exportResumeBtn(){
        MYGLoading.show()
        //type 1 简历模版 2生成附件简历  send_to 发送人邮箱
        MMYGNetwork<MYGResumeApi,MYGResumeModel>.request(target: .exportResume(send_to: "", type: 1)) { _ in
            MYGLoading.hide()

        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
    }
    
    func getResumList() {
        self.isEN = false
        self.contents = []
        MYGLoading.show()
        MMYGNetwork<MYGResumeApi,MYGResumeModel>.request(target: .personalInfo) { [self] item in
            MYGLoading.hide()
            
            guard let model = item else{return}
            self.doctorResumeModel = model
            self.headerView.model = model.doctor
//            self.contents = model.extModel

//            self.contents = model.extModel.filter{$0.isShow == 1 }
            model.extModel.forEach { m in
                if m.modelName != "Internship" {
                    let arr = m.list.filter{$0.is_show == 1 || $0.title.isEmpty == false }
                    let reModel = MYGResumType()
                    reModel.index = m.index
                    reModel.modelName = m.modelName
                    reModel.isShow = m.isShow
                    reModel.list.append(contentsOf: arr)
                    self.contents.append(reModel)
                }
            }
            if isMedia {
                getMediaResumInfo()
            }
            
            tableview.reloadData()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    func getCNResumInfo() {
        self.isEN = false
        self.headerView.isEN = false
        isMedia = false
        tableview.reloadData()
    }
    
    func getENResumInfo() {
        self.isEN = true
        isMedia = false
        self.headerView.isEN = true
        tableview.reloadData()
    }
    func getMediaResumInfo(){
        isMedia = true
        self.isEN = false
        self.headerView.isEN = false
        
        let item = MYGResumType()
        item.modelName = "自我介绍"
        let model = MYGResumeAllType()
        model.content = doctorResumeModel.media.content
        model.en_content = doctorResumeModel.media.content
        item.list = [model]
        
        mediaContents = [item]
        
        self.tableview.reloadData()
        
    }
    
    func editResumeInfo(model:MYGResumType,i:Int){
        let type = model.modelName
        var index = 5

        if  type == "Learnings" {
            let vc = MYGResumeEditController()
            
            vc.model = self.doctorResumeModel
            vc.learnS = [model.list[i]]
            vc.secondStep()
            navigationController?.pushViewController(vc)
            
        } else if (type == "Works"){
            let vc = MYGResumeEditController()
            vc.model = self.doctorResumeModel
            vc.workS = [model.list[i]]
            vc.threeStep()
            navigationController?.pushViewController(vc)
        }else {
            if (type == "GoodAt"){// 专业擅长
                index = 4
            }else if (type == "Honors"){//个人荣誉
                index = 0
            }else if (type == "Clinical"){//临床实验
                index = 5
//            }else if (type == "Internship"){//实习经验
//                index = 5
            } else if (type == "Publish"){//出版
                index = 2
            }else if (type == "Patent"){//获得专利
                index = 1
            } else if (type == "Custom"){//自定义模块
                index = 5
            }
            let vc = MYGResumeEditEndViewController()
            vc.skills = [model]
            vc.configCell(index)
            navigationController?.pushViewController(vc)
        }
        
    }
    
}


extension MYGResumeViewController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        isMedia ? self.mediaContents.count : self.contents.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let list = isMedia ? self.mediaContents[section] : self.contents[section]
        return list.list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let models = isMedia ? self.mediaContents[indexPath.section] : self.contents[indexPath.section]
        
        if isMedia {
            let cell = tableView.dequeueReusableCell(withClass: MYGMediaTableViewCell.self, for: indexPath)
            cell.isEN = self.isEN
            cell.type = models.modelName
            cell.model = models.list[indexPath.row]
            return cell
        }else {
            if models.modelName == "Learnings" || models.modelName == "Works" {
                let cell = tableView.dequeueReusableCell(withClass: MYGResumeContentCell.self, for: indexPath)
                cell.isEN = self.isEN
                cell.type = models.modelName
                cell.model = models.list[indexPath.row]
                return cell
            }
            else {
                let cell = tableView.dequeueReusableCell(withClass: MYGResumeOtherCell.self, for: indexPath)
                cell.isEN = self.isEN
                cell.type = models.modelName
                cell.model = models.list[indexPath.row]
                return cell
                
            }
        }
        
       
       
//        cell.editResumeInfBlock = {[self] in
//            editResumeInfo(model: models,i:indexPath.row)
//        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let models = isMedia ? self.mediaContents[indexPath.section] : self.contents[indexPath.section]
        let model = models.list[indexPath.row]
        
        if models.modelName == "Learnings" || models.modelName == "Works" {
            let str = models.modelName == "Learnings" ? model.experience : model.content
            let size = getStringSize(font: .regular(16), viewSize: CGSize(width: screenWidth - 45, height: CGFLOAT_MAX), str: str)

            return size.height + 78
        }
        
        if models.modelName == "GoodAt" {
            let str = models.modelName == "Learnings" ? model.experience : model.content
            let size = getStringSize(font: .regular(16), viewSize: CGSize(width: screenWidth - 45, height: CGFLOAT_MAX), str: str)

            return size.height + 30
        }
        
        let size = getStringSize(font: .regular(16), viewSize: CGSize(width: screenWidth - 175, height: CGFLOAT_MAX), str: model.title)

        return size.height + 50
    }
   
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView(frame: CGRect(x: 0, y: 0, width: Int(screenWidth), height: 30))
        
        let line = UIView()
        line.backgroundColor = .rgba(73, 117, 236, 1)
        v.addSubview(line)
        
        let tipLab = UILabel()
        tipLab.text = ""
        tipLab.textColor = .rgba(34, 34, 34, 1)
        tipLab.font = .medium(18)
        
        v.addSubview(tipLab)
        line.snp.makeConstraints { make in
            make.left.equalTo(17.5)
            make.height.equalTo(15)
            make.width.equalTo(3)
            make.top.equalToSuperview().offset(17.5)
        }
        
        tipLab.snp.makeConstraints { make in
            make.centerY.equalTo(line)
            make.left.equalTo(line.snp.right).offset(13)
            make.height.equalTo(25)
        }
        let list = isMedia ? self.mediaContents[section] : self.contents[section]
        let type = list.modelName
        if (type == "Learnings"){//教育
            tipLab.text = "教育经历"
        } else if (type == "Works"){//工作
            tipLab.text = "工作经历"
        } else if (type == "GoodAt"){// 专业擅长
            tipLab.text = "专业擅长"
        }else if (type == "Honors"){//个人荣誉
            tipLab.text = "个人荣誉"
        }
        else if (type == "Clinical"){//临床实验
            tipLab.text = "文献"
//        }
//        else if (type == "Internship"){//实习经验
//            tipLab.text = "实习经验"
        } else if (type == "Publish"){//出版
            tipLab.text = "出版"
        }else if (type == "Patent"){//获得专利
            tipLab.text = "获得专利"
        } else if (type == "Custom"){//自定义模块
            tipLab.text = "自定义"
        }else if (type == "自我介绍"){//自定义模块
            tipLab.text = "自我介绍"
        }
            
        return v
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
}
